//
//  JMWriteCodeCell.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/30.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMWriteCodeCell.h"
@interface JMWriteCodeCell ()

@property (weak, nonatomic) UITextField *textField;
@property (weak, nonatomic) UIView *selectionView;

@end

@implementation JMWriteCodeCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 设置图像
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 25, 25)];
        [self.contentView addSubview:iconView];
        self.iconVIew = iconView;
        
        // 设置Label
        UILabel *keyNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 60, 50)];
        keyNameLabel.textColor = JMColor(51, 51, 51);
        self.label = keyNameLabel;
        [self.contentView addSubview:keyNameLabel];
        
        // 设置textField
        UITextField *textField = [[UITextField alloc] init];
        [self.contentView addSubview:textField];
        self.inputField = textField;
        textField.tintColor = JMColor(153, 153, 153);
        CGFloat inputFieldW = 200;
        CGFloat inputFieldX = JMPhoneWidth - inputFieldW - 10;
        textField.frame = CGRectMake(inputFieldX, 2, inputFieldW, 50);
        textField.textAlignment = NSTextAlignmentRight;
        textField.font = [UIFont systemFontOfSize:12];
        textField.textColor = JMColor(51, 51, 51);
        [textField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingDidBegin];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidEndEditingNotification object:textField];
        self.textField = textField;
        
        // 去掉cell选中时的背景颜色
        self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 添加分割线
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 54, JMPhoneWidth, 1);
        view.backgroundColor = JMColor(242, 242, 242);
        [self.contentView addSubview:view];
        self.height = 55;
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        UIView *selectionView = [[UIView alloc] init];
        selectionView.frame = CGRectMake(0, 0, self.width, self.height);
        selectionView.backgroundColor = [UIColor whiteColor];
        self.selectedBackgroundView = selectionView;

    }
    return self;
}

+ (JMWriteCodeCell *)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"writecodeCell";
    JMWriteCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[JMWriteCodeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

/**
 *  开始编辑字体变大
 */
- (void)textDidChange:(UITextField *)textField {
    textField.font = [UIFont systemFontOfSize:14];
    self.backgroundColor = JMColor(245, 245, 245);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([self.delegate respondsToSelector:@selector(writeCodeCell:didSelectedRows:)]) {
        [self.delegate writeCodeCell:self didSelectedRows:0];
    }
}

/**
 *  结束编辑如果没有字，则字体变小
 */
- (void)textDidChange{
    if (!self.textField.text.length) {
        self.textField.font = [UIFont systemFontOfSize:12];
    }
    self.backgroundColor = [UIColor whiteColor];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
